package com.junlong0716.enjoylearning.ui

import android.content.Intent
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.afollestad.materialdialogs.MaterialDialog
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.adapter.DownloadFileAdapter
import com.junlong0716.enjoylearning.adapter.StudyHistoryAdapter
import com.junlong0716.enjoylearning.entity.DownloadFileEntity
import com.junlong0716.enjoylearning.manager.UserManager
import com.junlong0716.getknowledgedemo.decoration.DividerItemDecoration
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.activity_download.*

import kotlinx.android.synthetic.main.view_title_bar.*
import java.io.File
import java.util.regex.Pattern

class DownloadActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var downloadFileAdapter: DownloadFileAdapter
    private lateinit var downloadFile: ArrayList<DownloadFileEntity>
    private lateinit var filesInDir: List<File>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_download)

        rl_back.setOnClickListener(this)
        tv_title.text = "我的下载"
        downloadFile = ArrayList()
        downloadFileAdapter = DownloadFileAdapter(downloadFile)
        rv_download.layoutManager = LinearLayoutManager(this)
        rv_download.adapter = downloadFileAdapter
        downloadFileAdapter.emptyView = LayoutInflater.from(this).inflate(R.layout.view_empty, null)



        getAllOfDirFiles()

        downloadFileAdapter.setOnItemClickListener { adapter, view, position ->

            when (adapter.getItemViewType(position)) {
                DownloadFileEntity.DOWNLOAD_FILE_VIDEO -> {
                    //GO PLAY THE VIDEO
                    val videoIntent = Intent(this@DownloadActivity, PlayDownloadVideoActivity::class.java)
                    videoIntent.putExtra("DOWNLOAD_VIDEO_FILE", downloadFile[position].file)
                    startActivity(videoIntent)
                }
                DownloadFileEntity.DOWNLOAD_FILE_AUDIO -> {
                    //todo 暂时先外部打开 带加入数据库
                    val intent = Intent("android.intent.action.VIEW")
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                    intent.putExtra("oneshot", 0)
                    intent.putExtra("configchange", 0)
                    val uri = Uri.fromFile(downloadFile[position].file)
                    intent.setDataAndType(uri, "audio/*")
                    startActivity(intent)
                }
                else -> {
                }
            }
        }
        downloadFileAdapter.setOnItemLongClickListener { adapter, view, position ->
            MaterialDialog.Builder(this)
                    .title("是否删除？")
                    .canceledOnTouchOutside(false)
                    .positiveText("是")
                    .negativeText("否")
                    .onPositive { dialog, which ->
                        FileUtils.deleteFile(downloadFile[position].file)
                        getAllOfDirFiles()
                    }
                    .onNegative { dialog, which ->
                        dialog.dismiss()
                    }
                    .show()
            return@setOnItemLongClickListener true
        }
    }


    private fun getAllOfDirFiles() {
        downloadFile.clear()
        if (FileUtils.createOrExistsDir(Environment.getExternalStorageDirectory().absolutePath + File.separator + "享学知识缓存文件" + File.separator)) {
            filesInDir = FileUtils.listFilesInDir(Environment.getExternalStorageDirectory().absolutePath + File.separator + "享学知识缓存文件" + File.separator)
        }
        rv_download.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL, 2, ContextCompat.getColor(this, R.color.recyclerview_divider_gray)))
        //获取文件夹路径


        for (file in filesInDir) {
            //文件分组
            when (FileUtils.getFileExtension(file)) {
                "mp3" -> {
                    downloadFile.add(DownloadFileEntity(2, file))
                }
                "mp4" -> {
                    downloadFile.add(DownloadFileEntity(1, file))
                }
            }
        }


        downloadFileAdapter.notifyDataSetChanged()
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.rl_back -> {
                finish()
            }
            else -> {
            }
        }
    }
}
